# -*- coding: utf-8 -*-
"""
TG滚动区域组件 - TgScrollArea
滚动区域容器组件，支持内容滚动显示
"""
from PySide6.QtWidgets import QScrollArea, QFrame
from PySide6.QtCore import Qt
from ...style_manager import StyleManager


class TgScrollArea(QScrollArea):
    """TG滚动区域组件"""
    
    def __init__(self, parent=None):
        super().__init__(parent)
        # 初始化TG组件属性
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
        
        # 初始化组件
        self.initialize()
    
    @property
    def component_name(self):
        """获取组件名称"""
        return self._component_name
    
    @property
    def is_initialized(self):
        """检查组件是否已初始化"""
        return self._is_initialized
    
    @property
    def style_applied(self):
        """检查样式是否已应用"""
        return self._style_applied
    
    def setup_ui(self):
        """设置用户界面"""
        # 设置滚动区域属性
        self.setWidgetResizable(True)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
        # 设置框架样式
        self.setFrameShape(QFrame.Shape.NoFrame)
    
    def set_horizontal_scroll_policy(self, policy):
        """设置水平滚动条策略
        
        Args:
            policy: 滚动条策略，可选值：
                   'always_off' - 始终关闭
                   'always_on' - 始终显示
                   'as_needed' - 根据需要显示
        """
        policy_map = {
            'always_off': Qt.ScrollBarPolicy.ScrollBarAlwaysOff,
            'always_on': Qt.ScrollBarPolicy.ScrollBarAlwaysOn,
            'as_needed': Qt.ScrollBarPolicy.ScrollBarAsNeeded
        }
        if policy in policy_map:
            self.setHorizontalScrollBarPolicy(policy_map[policy])
    
    def set_vertical_scroll_policy(self, policy):
        """设置垂直滚动条策略
        
        Args:
            policy: 滚动条策略，可选值：
                   'always_off' - 始终关闭
                   'always_on' - 始终显示
                   'as_needed' - 根据需要显示
        """
        policy_map = {
            'always_off': Qt.ScrollBarPolicy.ScrollBarAlwaysOff,
            'always_on': Qt.ScrollBarPolicy.ScrollBarAlwaysOn,
            'as_needed': Qt.ScrollBarPolicy.ScrollBarAsNeeded
        }
        if policy in policy_map:
            self.setVerticalScrollBarPolicy(policy_map[policy])
    
    def apply_style(self):
        """应用样式"""
        self.setStyleSheet(f"""
            QScrollArea {{
                border: 1px solid {StyleManager.COLORS['border']};
                border-radius: {StyleManager.BORDER_RADIUS['medium']};
                background-color: {StyleManager.COLORS['background']};
            }}
            QScrollBar:vertical {{
                background-color: {StyleManager.COLORS['surface']};
                width: 12px;
                border-radius: 6px;
                margin: 0;
            }}
            QScrollBar::handle:vertical {{
                background-color: {StyleManager.COLORS['border']};
                border-radius: 6px;
                min-height: 20px;
                margin: 2px;
            }}
            QScrollBar::handle:vertical:hover {{
                background-color: {StyleManager.COLORS['text_secondary']};
            }}
            QScrollBar::handle:vertical:pressed {{
                background-color: {StyleManager.COLORS['text_primary']};
            }}
            QScrollBar::add-line:vertical,
            QScrollBar::sub-line:vertical {{
                height: 0px;
            }}
            QScrollBar:horizontal {{
                background-color: {StyleManager.COLORS['surface']};
                height: 12px;
                border-radius: 6px;
                margin: 0;
            }}
            QScrollBar::handle:horizontal {{
                background-color: {StyleManager.COLORS['border']};
                border-radius: 6px;
                min-width: 20px;
                margin: 2px;
            }}
            QScrollBar::handle:horizontal:hover {{
                background-color: {StyleManager.COLORS['text_secondary']};
            }}
            QScrollBar::handle:horizontal:pressed {{
                background-color: {StyleManager.COLORS['text_primary']};
            }}
            QScrollBar::add-line:horizontal,
            QScrollBar::sub-line:horizontal {{
                width: 0px;
            }}
        """)
        self._style_applied = True
    
    def initialize(self):
        """初始化组件"""
        if not self._is_initialized:
            self.setup_ui()
            self.apply_style()
            self._is_initialized = True
            self._style_applied = True
    